<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页刮刮卡</title>
    <style>
        .img {
            width: 400px;
            height: 300px;
            position: absolute;
            left: 200px;
            z-index: -1;
        }
        canvas {
            margin-left: 200px;
            position: absolute;
            top: 0;
            z-index: 1;
        }
        body {
            margin: 0;
        }
    </style>
</head>
<body>
    <div class="img">
        <img src="./prize.png" style="width:100%;height:100%;" />
    </div>
    <canvas id="canvas" width="400" height="300"></canvas>
    <script>
        let canvas = document.querySelector("#canvas");
        let context = canvas.getContext("2d");

        // Create a transparent gray mask for the scratch area
        context.fillStyle = "rgba(200, 200, 200, 0.8)"; // Gray color with opacity
        context.fillRect(0, 0, canvas.width, canvas.height);

        // Store the positions of the scratched areas
        let scratchedArea = [];

        // Set up mousemove event listener to remove parts of the mask
        canvas.addEventListener("mousemove", (e) => {
            if (e.buttons === 1) {  // Only when the left mouse button is pressed
                const rect = canvas.getBoundingClientRect();
                const x = e.clientX - rect.left;
                const y = e.clientY - rect.top;

                // Remove part of the gray mask using 'destination-out' mode
                context.globalCompositeOperation = "destination-out";
                context.beginPath();
                context.arc(x, y, 10, 0, Math.PI * 2);
                context.fill();

                // Save the scratched coordinates for area calculation
                scratchedArea.push({ x, y });
                let uncovered = calculateUncoveredArea();
                
                // Check if more than 50% is uncovered and alert the user
                if (uncovered > 50) {
                    alert("You have uncovered more than 50% of the card!");
                }
            }
        });

        // Function to calculate the percentage of the uncovered area
        function calculateUncoveredArea() {
            // Create a bounding box to calculate the area
            let coveredPixels = 0;
            let totalPixels = canvas.width * canvas.height;
            for (let i = 0; i < scratchedArea.length; i++) {
                let x = scratchedArea[i].x;
                let y = scratchedArea[i].y;
                if (context.getImageData(x, y, 1, 1).data[3] === 0) {
                    coveredPixels++;  // Count the uncovered pixel
                }
            }
            return (coveredPixels / totalPixels) * 100;
        }
    </script>
</body>
</html>
